package com.lagou.service;

import com.lagou.handler.JsonMsgHandler;
import com.lagou.util.JsonMsg;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

public class UserServiceImpl implements UserService {

    public String sayHello(String word) {
        System.out.println("111111111111调用成功--参数 " + word);
        return "222222222222222222222222调用成功--参数 " + word;
    }

    public String sayHello2(JsonMsg jsonMsg) {
        return null;
    }

    //hostName:ip地址  port:端口号
    public  void startServer(String hostName, int port) throws InterruptedException {

        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();

        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast(new StringEncoder());
                        pipeline.addLast(new StringDecoder());
                        pipeline.addLast(new StringDecoder());
                        ch.pipeline().addLast(new JsonMsgHandler());
//                        pipeline.addLast(new UserServerHandler());

                    }
                });
        serverBootstrap.bind(hostName, port).sync();
        register(hostName, String.valueOf(port));

    }

    private  CuratorFramework curatorFramework;
    protected static final String FOLDER = "/serverBootstrap";
    protected static final String SEPARATOR = "/";
    private static final int SESSION_TIMEOUT_MS = 5000;
    private static final int SLEEP_TIME_MS = 1000;
    private static final int MAX_RETRIES = 2;

    public void init(){
        curatorFramework = CuratorFrameworkFactory.builder()
                .connectString("127.0.0.1:2181")
                .sessionTimeoutMs(SESSION_TIMEOUT_MS)
                .retryPolicy(new ExponentialBackoffRetry(SLEEP_TIME_MS, MAX_RETRIES))
                .build();
        curatorFramework.start();

    }
    public  void register(String hostName, String port) {
        try {
            init();
            String servicePath = FOLDER + SEPARATOR + hostName + ":" + port;
            Stat stat = curatorFramework.checkExists().forPath(servicePath);
            if (stat == null) {
                curatorFramework.create().creatingParentsIfNeeded()
                        .withMode(CreateMode.PERSISTENT).forPath(servicePath);
            }
            curatorFramework.create().withMode(CreateMode.EPHEMERAL)
                    .forPath(servicePath);
        } catch (Exception e) {
            System.out.println("异常 " + e);

        }
    }
}
